package com.mesSystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mesSystem.dto.Result;
import com.mesSystem.entity.EquipmentMaintenance;
import com.mesSystem.entity.EquipmentRepair;
import com.mesSystem.service.impl.EquipmentRepairService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@ResponseBody
@Slf4j
@RequestMapping("equipment/equipmentRepair")
public class EquipmentRepairController {
    @Autowired
    EquipmentRepairService equipmentRepairService;

    /**
     * 新增--设备维修
     * @param equipmentRepair
     * @return
     */
    @PostMapping("increase")
    public Result increase(@RequestBody EquipmentRepair equipmentRepair) {
        log.info(equipmentRepair.toString());
        equipmentRepairService.save(equipmentRepair);
        return Result.ok("创建成功");
    }

    @DeleteMapping("delete")
    public Result delete(Long equipmentRepairId) {
        equipmentRepairService.removeById(equipmentRepairId);
        return Result.ok("删除成功");
    }

    /**
     * 根据设备id，分页查询设备故障维修信息
     */
    @GetMapping("query")
    public Result query(@RequestParam(value = "page", defaultValue = "1") Integer page,
                        @RequestParam(value = "size", defaultValue = "10") Integer size,
                        @RequestParam(value = "equipmentId") Long equipmentId) {
        // 创建分页器对象
        Page<EquipmentRepair> Page = new Page<>(page, size);

        // 调用分页查询方法，将分页器传递给service
        equipmentRepairService.page(Page, new QueryWrapper<EquipmentRepair>().eq("equipment_id",equipmentId).orderByAsc("repair_date"));

        // 从分页器对象中获取分页后的数据
        List<EquipmentRepair> list = Page.getRecords();

        log.info("被查询信息为：{}", list);

        // 返回分页结果
        return Result.ok(list);
    }

    /**
     * 更改
     */
    @PostMapping("update")
    public Result update(@RequestBody EquipmentRepair equipmentRepair) {
        equipmentRepairService.updateById(equipmentRepair);
        log.info("update:{}", equipmentRepair);
        return Result.ok("更新成功");
    }
}
